﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using DAO;
using BUS;
using AjaxChatService.Models;

namespace AjaxChatService.Controllers
{
    [HttpHeader("Access-Control-Allow-Origin", "*")]
    public class RoomRequestsController : ApiController
    {
        RoomRequestBUS roomRequestBUS = new RoomRequestBUS();

        // GET api/RoomRequests/5
        public HttpResponseMessage GetRoomRequest(int userId)
        {
            RoomRequest roomRequest = roomRequestBUS.GetRoomRequest(userId);
            if (roomRequest == null)
                return Request.CreateResponse(HttpStatusCode.NotFound);

            RoomRequestModel model = new RoomRequestModel
                                    {
                                        RoomId = roomRequest.RoomId.GetValueOrDefault(),
                                        RoomName = roomRequest.ChatRoom.Name,
                                        UserId = roomRequest.UserId.GetValueOrDefault(),
                                        UserName = roomRequest.ChatRoom.User.UserName
                                    };

            return Request.CreateResponse(HttpStatusCode.OK, model);
        }

        // POST api/RoomRequests
        public HttpResponseMessage PostRoomRequest(int userId, string friendName, int roomId)
        {
            if (roomRequestBUS.AddRoomRequest(userId, friendName, roomId))
                return Request.CreateResponse(HttpStatusCode.OK);

            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }

        public HttpResponseMessage PostDeleteRoomRequest(int userId, int roomId)
        {
            if(roomRequestBUS.DeleteRoomRequest(userId, roomId))
                return Request.CreateResponse(HttpStatusCode.OK);

            return Request.CreateResponse(HttpStatusCode.BadRequest); 
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}